package com.fone.player.activity.personal.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.personal.CacheActivity;
import com.fone.player.client.DramaRst.Cnts;
import com.fone.player.util.L;

/**
 * 分页选择下载条使用的adapter
 * 
 * @author JasonZue
 * @since 2014-4-29
 */

public class CacheTvPageAdapter extends BaseAdapter {

	private List<Cnts> mPageData;
	private Context mContext;
	private int selectedPostion = 0;// 默认选中第一个
	private Handler mHandler;

	public CacheTvPageAdapter(Context mContext, List<Cnts> pageData,
			int selectedPagePosition, Handler mHandler) {
		this.mPageData = pageData;
		this.mContext = mContext;
		this.selectedPostion = selectedPagePosition;
		this.mHandler = mHandler;

	}

	public void resetSelectedPostion(){
		selectedPostion=0;
	}
	@Override
	public int getCount() {
		
		return mPageData.size();

	}

	@Override
	public Object getItem(int position) {
		return mPageData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext,
					R.layout.personal_cache_page_item, null);

			holder.itemNUM = (TextView) convertView
					.findViewById(R.id.cache_page_num_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		L.v("JasonZue", "position:" + position);
		holder.itemNUM.setText(mPageData.get(position).name);
		holder.itemNUM.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				L.v("JasonZue", "响应text");
				
				selectedPostion = position;
				notifyDataSetInvalidated();

				Message message = mHandler.obtainMessage();
				message.what = CacheActivity.CHANGE_PAGE;
				message.arg1 = position;
				message.obj = mPageData.get(position).url;
				mHandler.sendMessage(message);
			}
		});
		if (selectedPostion == position) {
			holder.itemNUM.setTextColor(mContext.getResources().getColor(
					R.color.cache_page_text_orange_color));
		} else {
			holder.itemNUM.setTextColor(mContext.getResources().getColor(
					R.color.cache_page_text_grey_color));
		}

		return convertView;

	}
	

	class ViewHolder {
		public TextView itemNUM;

	}

}
